var kp =
{		
	rule:
	{
		user:/^[a-zA-z]\w{5,15}|(1[3|4|5|6|7|8][0-9]{9})|(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/,
		pwd:/^[\S]{6,16}$/
	},
	fontsize:function(size)
	{
		$("#article").css("font-size",size + "px");
	},
	show:function(id)
	{
		$("#imgX").attr("src",$("#img_" + id).attr("src"));
	},
	filter:function()
	{
		var path = window.location.pathname.split("/");
		path = path.filter(function(n) { return n; });
		path = "/" + path[0] + "/filter/sp";
		var sp = $("#sprice").val();
		var ep = $("#eprice").val();
		var ss = $("#ssale").val();
		var es = $("#esale").val();
		if(sp.length > 0)		
			path += "-" + sp;
		if(ep.length > 0)
			path += "-" + ep;
		if(ss.length > 0)
			path += "-" + ss;
		if(es.length > 0)
			path += "-" + es;
		location.href = path;
	},
	spinner:function(o,obj)
	{
		var num = $("#" + obj).val();
		num = parseInt(num);
		if(o == -1)
		{
			if(num > 1)
			num -= 1;			
		}
		else if(o == 1)
		{
			if(num < 999)
				num += 1;
		}	
		$("#" + obj).val(num)		;
	},
	setmoney:function(m)
	{
		tp = 0;
		total = 0;
		var obj = document.getElementsByName("cbx");
		for(var i = 0;i < obj.length;i++)
		{			
			if(!obj[i].checked && m != 'null')
				continue;
			else
			{
				tp = $("#total_" + i).html().replace("￥","").replace("元","");
				tp = parseFloat(tp);
				total += tp;
			}
		}
		$(".info").find("span.price").find("span").html(total.toFixed(2));
	},
	change:function(o,id,sum,price)
	{
		var total = 0;
		num = parseInt($("#num_" + id).val());	
		if(o == -1)
		{
			if(num > 1)
			num -= 1;			
		}
		else if(o == 1)
		{
			if(num < 999)
				num += 1;
		}		
		price *= num;		
		$("#num_" + id).val(num);
		$("#total_" + id).html("￥" + price.toFixed(2) + '元');
		kp.setmoney('null');
		
	},
	addfav:function(url, title)
	{
		try
		{
			window.external.addFavorite(url, title);
		}
		catch (e)
		{
			try
			{
				window.sidebar.addPanel(title, url, "");
			}
			catch (e)
			{
				alert("加入收藏失败，请使用Ctrl+D进行添加");
			}
		}
	},	
	get:function(o)
	{
		var para = "";
		var obj = document.getElementsByName(o);
		for (i = 0; i < obj.length; i++)
		{
			if ((obj[i].checked))
			{
				para += obj[i].value + ",";
			}
			else
			{
				para += "";
			}
		}
		return para.substring(0, para.length - 1);
	},
	selectall:function()
	{
		var o = document.getElementById("slt").checked;
		var obj = document.getElementsByName("cbx");
		if (o)
		{
			if (obj.length <= 0)
			{
				return false;
			}
			else
			{
				if (typeof (obj.length) == "undefined")
				{
					obj.checked = true;
				}
				else
				{
					for (var i = 0; i < obj.length; i++)
					{
						obj[i].checked = true;
					}
				}
			}
		}
		else
		{
			if (typeof (obj.length) == "undefined")
			{
				obj.checked = false;
			}
			else
			{
				for (var i = 0; i < obj.length; i++)
				{
					obj[i].checked = false;
				}
			}
		}
		kp.setmoney('');
	},
	login:function()
	{
		var url = '/handle.php?module=member&action=login';
		var uname = $("#username");
		var upwd = $("#password");
		var codex = $("#codex"); 
		var remmber = $("#remmber");
		var flag = 0;
		if(!kp.rule.user.test(uname.val()))
		{
			$("#showmsg").html('<i></i>请填写正确的用户名/手机号码/邮箱地址').addClass("errmsg");
			uname.focus();
			return false;
		}
		if(!kp.rule.pwd.test(upwd.val()))
		{
			$("#showmsg").html('<i></i>请填写登录密码').addClass("errmsg");
			upwd.focus();
			return false;
		}
		if(codex.val().length < 4)
		{
			$("#showmsg").html('<i></i>验证码不正确').addClass("errmsg");
			codex.focus();
			return false;
		}
		if(remmber.attr("checked") == "checked")
			flag = 1;
		$("#showmsg").html('<i></i>请稍候...').addClass("errmsg");
		var data = {"uid": uname.val(), "pwd": upwd.val(),"flag":flag,"code":codex.val()};
		kp.ajaxTodo(url,data);		
	},
	register:function()
	{
		var url = '/handle.php?module=member&action=register';
		var uname = $("#username");
		var upwd = $("#password");
		var codex = $("#codex"); 
		var remmber = $("#remmber");
		var flag = 0;
		if(!kp.rule.user.test(uname.val()))
		{
			$("#showmsg").html('<i></i>请填写正确的用户名/手机号码/邮箱地址').addClass("errmsg");
			uname.focus();
			return false;
		}
		if(!kp.rule.pwd.test(upwd.val()))
		{
			$("#showmsg").html('<i></i>请填写登录密码').addClass("errmsg");
			upwd.focus();
			return false;
		}
		if(upwd.val() != $("#cpassword").val())
		{
			$("#showmsg").html('<i></i>两次密码不一致').addClass("errmsg");
			upwd.focus();
			return false;
		}
		if(codex.val().length < 4)
		{
			$("#showmsg").html('<i></i>验证码不正确').addClass("errmsg").show();
			codex.focus();
			return false;
		}
		if(remmber.attr("checked") != "checked")
		{
			$("#showmsg").html('<i></i>没有选择同意服务协议').addClass("errmsg").show();
			return false;
		}
		$("#showmsg").html('<i></i>请稍候...').addClass("errmsg");
		var data = {"uid": uname.val(), "pwd": upwd.val(),"code":codex.val()};
		kp.ajaxTodo(url,data);
	},	
	findx:function()
	{
		var url = '/handle.php?module=member&action=findx';
		var uname = $("#username");
		var codex = $("#codex"); 
		if(!kp.rule.user.test(uname.val()))
		{
			$("#showmsg").html('<i></i>请填写正确的用户名/手机号码/邮箱地址').addClass("errmsg");
			uname.focus();
			return false;
		}
		if(codex.val().length < 4)
		{
			$("#showmsg").html('<i></i>验证码不正确').addClass("errmsg").show();
			codex.focus();
			return false;
		}
		var data = {"uid": uname.val(),"code":codex.val()};
		kp.ajaxTodo(url,data);
	},
	findy:function()
	{
		var url = '/handle.php?module=findx&action=findy';
		var question = $("#question");
		var answer = $("#answer");
		var codex = $("#codex"); 
		if(question.val().length <= 0)
		{
			$("#showmsg").html('<i></i>请填写密码提示问题').addClass("errmsg");
			question.focus();
			return false;
		}
		if(answer.val().length <= 0)
		{
			$("#showmsg").html('<i></i>请填写密码提示答案').addClass("errmsg");
			answer.focus();
			return false;
		}
		if(codex.val().length < 4)
		{
			$("#showmsg").html('<i></i>验证码不正确').addClass("errmsg").show();
			codex.focus();
			return false;
		}
		var data = {"qst": question.val(),"ans":answer.val(),"code":codex.val()};
		kp.ajaxTodo(url,data);
	},
	findz:function()
	{
		var url = '/handle.php?module=member&action=findz';
		var upwd = $("#password");
		var codex = $("#codex"); 
		var flag = 0;		
		if(!kp.rule.pwd.test(upwd.val()))
		{
			$("#showmsg").html('<i></i>请填写新密码').addClass("errmsg");
			upwd.focus();
			return false;
		}
		if(upwd.val() != $("#cpassword").val())
		{
			$("#showmsg").html('<i></i>两次密码不一致').addClass("errmsg");
			upwd.focus();
			return false;
		}
		if(codex.val().length < 4)
		{
			$("#showmsg").html('<i></i>验证码不正确').addClass("errmsg").show();
			codex.focus();
			return false;
		}		
		$("#showmsg").html('<i></i>请稍候...').addClass("errmsg");
		var data = {"pwd": upwd.val(),"code":codex.val()};
		kp.ajaxTodo(url,data);
	},
	addcar:function(num,id,price,title,litpic,url)
	{
		
		var path = '/handle.php?module=shopcar&action=addshopcar';
		var data = {"num":num,"id":id,"price":price,"title":title,"litpic":litpic,"url":url};
		$.getJSON(path,data,kp.init);
	},
	delcar:function(id)
	{
		
		var op = {"id":id};
		var url = '/handle.php?module=shopcar&action=delcar';		
		$.getJSON(url, op, function (data)
		{
			kp.init();
			if($("table.list"))
				kp.carlist();
		})
	},
	carlist:function()
	{
		var path = '/handle.php?module=shopcar&action=shopcar';
		$.getJSON(path,null,function(data)
		{
			tpl = '';
			total = 0;
			price = 0.0;
			for(var i = 0;i < data.length;i++)
			{
				total += parseInt(data[i].num);
				price += parseFloat(data[i].price) * data[i].num;
				tpl += '<tr id="list_' + i + '">';
				tpl += '	<td class="check"><input checked="checked" name="cbx" value="' + i +'" onchange="kp.setmoney('+i+','+data.length+')" type="checkbox" id="cbx_'+i+'"/></td>';
				tpl += '	<td class="info"><a href="' + data[i].url + '" class="img"><img src="' + data[i].litpic + '"/>' + data[i].title + '</a></td>';
				tpl += '	<td class="price">￥' + data[i].price + '</td>';
				tpl += '	<td class="price">￥' + data[i].price + '</td>';
				tpl += '	<td class="num">';
				tpl += '		<div>';
				tpl += '			<a href="javascript:void(0);" data="' + data[i].price + '" class="decrement" onclick="kp.change(-1,'+i+','+data.length+','+data[i].price+')"></a>';
				tpl += '			<input class="ipt" value="' + data[i].num + '" type="text" id="num_'+i+'" maxlength="3" onblur="kp.change(this.value,'+i+','+data.length+','+data[i].price+')"/>'
				tpl += '			<a href="javascript:void(0);" data="' + data[i].price + '" class="increment" onclick="kp.change(1,'+i+','+data.length+','+data[i].price+')"></a>';
				tpl += '		</div>';
				tpl += '	</td>';
				tpl += '	<td class="total" id="total_' + i +'">￥' + (parseFloat(data[i].price) * data[i].num).toFixed(2) + '元</td>';
				tpl += '	<td class="opera"><a href="javascript:void(0)" onclick="kp.delcar(' + i + ')">删除</a></td>';
				tpl += '</tr>';
			}
			$("table.list").html(tpl);
			$(".info").find("span.price").find("span").html(price.toFixed(2));
		});
	},
	init:function()
	{
		tpl = '';
		var op = null;
		var url = '/handle.php?module=member&action=chklogin';
		$.getJSON(url, op, function (data)
		{		
			if(data.statusCode == 200)
				tpl = '<i><img src="' + data.face +'"></i><em>Hi：' + data.uname + '，<b>LV' + data.rank + '</b></em><span><a href="/ucenter">进入会员中心</a></span><label><font>积分：<b>' + data.score + '</b></b><tt>|</tt><font>余额：<b>' + data.money + '</b></font></label>';
			else
				tpl = '<i><img src="/skin/images/uc.png"></i><em>Hi：你好，</em><span>点击<a href="/login">登录</a>，签到领积分</span><label><a href="/item/help">如何赚积分？</a><tt>|</tt><a href="/register">注册新用户</a></label>';			
				$("#ucenter").html(tpl);
		})		
		$.getJSON('/handle.php?module=shopcar&action=shopcar',null,function(data)
		{
			tpl = '';
			total = 0;
			price = 0.0;
			for(var i = 0;i < data.length;i++)
			{
				total += parseInt(data[i].num);
				price += parseFloat(data[i].price) * data[i].num;
				tpl += '<li id="list_' + i + '">';
				tpl += '	<label>';
				tpl += '		<a href="' + data[i].url + '" class="img"><img src="' + data[i].litpic + '"/>' + data[i].title + '</a>';
				tpl += '	</label>';
				tpl += '	<label class="r">'
				tpl += '		<em>￥' + data[i].price + ' * ' + data[i].num + '</em>';
				tpl += '		<a href="javascript:void(0)" onclick="kp.delcar(' + i + ')">删除</a>';
				tpl += '	</label>';									
				tpl += '</li>';
			}
			$(".shopcar").find("i").html(total);
			$(".shopcar").find("ul").html(tpl);
			$(".shopcar").find("font.total").html(total);
			$(".shopcar").find("font.price").html('￥' + price.toFixed(2) + '元');
		})
	},
	sethome:function(obj,vrl)
	{
		try
		{
			obj.style.behavior='url(#default#homepage)';
			obj.setHomePage(vrl);
		}
		catch(e)
		{
			if(window.netscape) 
			{
				try 
				{
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
				}
				catch (e) 
				{
					alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将 [signed.applets.codebase_principal_support]设置为'true'");
				}
				var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
				prefs.setCharPref('browser.startup.homepage',vrl);
			 }
		}
	},
	ajaxTodo:function(url,op)
	{
		$.getJSON(url, op, function (data)
		{		
			if (data.statusCode == "200")
			{	
				$("#showmsg").html(data.msg).addClass("sucmsg").show();
				setTimeout(function ()
				{
					location.href = data.url;
				}, 2000);
			}
			else 
			{
				$("#showmsg").html(data.msg).addClass("errmsg").show();
			}			
		})
	}
}
$(document).ready(function()
{
	kp.init();
	var cp = '';
	var path = '';
	var para = window.location.pathname;	
	var url = para.split("/")[1].split("/")[0];
	if(url == "")
		path = "/";
	else if(url == "item")
		path = "/item/about";
	else
		path = "/" + url;
	
	var pos = para.split("/");
	pos = pos.filter(function(n) { return n; });
	for(var t = 0;t < pos.length;t++)
	{
		if(t > 3)
			break;
		else
			cp += '/' + pos[t];
	}
	if(pos[1] != 'list')
		cp = '/' + pos[0];
	else
	{
		pos.pop();
		cp = '/' + pos.join('/') + "/1";
	}
	
	
	$("#menu a").removeClass("fixed");
	$("#menu a[href='" + path + "']").addClass("fixed");

	$("#navi a").removeClass("fixed");
	$("#navi a[href='" + cp + "']").addClass("fixed");
	
	$(".plat a").removeClass("fixed");
	$(".plat a[href='" + para + "']").addClass("fixed");
	if ($("#flashbox"))
	{
		$("#flashbox").slide({titCell:".hd a", mainCell: ".bd", effect: "fold",autoPlay:1, delayTime: 10000});
	}
	$("#gotop").click(function()
	{
		$("html,body").animate({'scrollTop':'0px'},500);
	});
	$("#hotkey").click(function()
	{
		key = $("#keyword");
		if(key.val().length <= 0)
		{
			key.attr("placeholder","请填写搜索关键字");
			key.focus();
			return false;
		}
		if(url != "activity" || url != "coupon" || url != "promotion")
			url = "/activity";		
		location.href = url + "/tag/" + key.val();
	})
	$(".shopcar .cart").mouseover(function()
	{
		$(this).siblings(".carlist").fadeIn();
	});
	$(".close").click(function()
	{
		$(".carlist").fadeOut();
	})
	if($("p.login"))
	{
		$("p.login").click(kp.login);
	}
	if($("p.register"))
	{
		$("p.register").click(kp.register);
	}
	if($("p.findx"))
	{
		$("p.findx").click(kp.findx);
	}
	if($("p.findy"))
	{
		$("p.findy").click(kp.findy);
	}
	if($("p.findz"))
	{
		$("p.findz").click(kp.findz);
	}
	if($(".product-detail"))
	{
		var tab = new switchtab
		({
			menus: $("#tabInner li"),
			cons: $("#tabContent .detail-item"),
			clsname: "active",
			cur: 0
		});
	}
})